<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.faf.bean.Food"%>
<%@page import="com.faf.bean.Orders"%>
<%@page import="com.faf.bean.Detail"%>
<%@page import="java.util.List"%>
<%@page import="java.text.DecimalFormat"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + path+"/"+"UserGlobal"+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>天天生鲜-用户中心</title>
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/main.css">
	<link rel="stylesheet" href="/FAFCode/UserManagement/css/sweetalert2.bundle.css">
</head>
<% 
	Object pre_global_info_user_name = request.getSession().getAttribute("account");
	String global_info_user_name;
	HashMap<Integer, Integer> global_info_cart_id2count = null;
	int i_food_count = 0;
	
	if(null != pre_global_info_user_name){
		global_info_user_name = pre_global_info_user_name.toString();
		
		global_info_cart_id2count = (HashMap<Integer, Integer>)request.getSession().getAttribute("global_cart");
		for(int value : global_info_cart_id2count.values())
			i_food_count += value;
		
	} else {
		global_info_user_name = "";
	}
	
	request.getSession().setAttribute("pre-page", "/MyOrders");
%>
<body>
<% 
	Object pre_myorder_dat = request.getAttribute("pre_myorder_dat");
	if(null == pre_myorder_dat) {
%>
	<jsp:forward page="/MyOrders"/>
<%
	} else { 
		DecimalFormat fm = new java.text.DecimalFormat("#.00");
		List<Orders> user_orders_orderlist = (List<Orders>)request.getAttribute("user_orders_orderlist");
		HashMap<Integer, Food> user_orders_id2food = (HashMap<Integer, Food>)request.getAttribute("user_orders_id2food");
		HashMap<String, List<Detail>> user_orders_detaillist = (HashMap<String, List<Detail>>)request.getAttribute("user_orders_detaillist");
%>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到饭否!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em><%= null == pre_global_info_user_name ? global_info_user_name : "请登录" %></em>
				</div>
				<% if(null == pre_global_info_user_name) { %>
				<div class="login_btn fl">
					<a href="/FAFCode/UserManagement/login.jsp">登录</a>
					<span>|</span>
					<a href="/FAFCode/UserManagement/register.jsp">注册</a>
				</div>
				<% } else { %>
				<div class="user_link fl">
					<a href="/FAFCode/UserManagement/user_center_info.jsp">用户中心</a>
					<span>|</span>
					<a href="/FAFCode/MyCart">我的购物车</a>
					<span>|</span>
					<a href="/FAFCode/UserManagement/user_center_order.jsp">我的订单</a>
				</div>
				<% } %>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="/FAFCode/Index" class="logo fl"><img src="/FAFCode/Image/logo/logo.png"></a>
		<form action="List" >
		<div class="search_con fl">
			<input type="text" class="input_text fl" name="serchContect" placeholder="搜索商品">
			<input type="submit" class="input_btn fr" name="" value="搜索">
		</div>
		</form>
		<div class="guest_cart fr">
			<a href="/FAFCode/MyCart" class="cart_name fl">我的购物车</a>
			<div class="goods_count fl" id="show_count"><%=i_food_count %></div>
		</div>
	</div>

	<div class="main_con clearfix">
		<div class="left_menu_con clearfix">
			<h3>用户中心</h3>
			<ul>
				<li><a href="/FAFCode/UserManagement/user_center_info.jsp" >· 个人信息</a></li>
				<li><a href="/FAFCode/UserManagement/user_center_order.jsp" class="active">· 全部订单</a></li>
				<li><a href="/FAFCode/UserManagement/user_center_site.jsp">· 收货地址</a></li>
				<li><a href="/FAFCode/UserManagement/user_center_update.jsp">· 个人信息修改</a></li>
				<li><a href="/FAFCode/UserManagement/user_center_pwd.jsp" >· 修改密码</a></li>
				
			</ul>
		</div>
		<div class="right_content clearfix">
				<h3 class="common_title2">全部订单</h3>
				
				<%
					for(Orders orders : user_orders_orderlist){
				%>
				<ul class="order_list_th w978 clearfix">
					<li class="col01"><%= orders.getOrdersTime() %></li>
					<li class="col02">订单号：<%= orders.getOrdersNumber() %></li>
					<%if(orders.getOrdersState() == -1){ %>
						<li class="col02 stress">未支付</li>
					<%}else if(orders.getOrdersState()== 0){ %>
						<li class="col02 stress">已支付，等待商家接单</li>
					<%}else if(orders.getOrdersState()==1){ %>
						<li class="col02 stress">商家已接单</li>
					<%}else if(orders.getOrdersState()==2){ %>
					<li class="col02 stress">商家已拒单</li>
					<%}else if(orders.getOrdersState()==3){ %>
					<li class="col02 stress">已签收</li>
					<% } %>
				</ul>

				<table class="order_list_table w980">
					<tbody>
						<tr>
							<td width="55%">
							<%
								double d_t_price = 0;
								List<Detail> detaillist = user_orders_detaillist.get(orders.getOrdersNumber());
								for(Detail detail : detaillist) {
									Food food = user_orders_id2food.get(detail.getFoodId());
									d_t_price += detail.getFoodNumber()*food.getFoodPrice();
							%>
								<ul class="order_goods_list clearfix">					
									<li class="col01"><img src="<%= "/FAFCode/"+food.getFoodPhoto() %>"></li>
									<li class="col02"><%=food.getFoodName() %><em><%=food.getFoodPrice() %>元/1份</em></li>	
									<li class="col03"><%=detail.getFoodNumber() %></li>
									<li class="col04"><%=fm.format(detail.getFoodNumber()*food.getFoodPrice()) %>元</li>	
								</ul>
								<%
								}
								%>
							</td>
							<td width="15%"><%=fm.format(d_t_price) %>元</td>
							<%
								if(orders.getOrdersState() == -1) {
							%>
							<td width="15%"><a href="<%="/FAFCode/PlaceOrder?order=" + orders.getOrdersNumber() %>" class="oper_btn">去付款</a></td>
							<%
								} else if(orders.getOrdersState() == 0) {
							%>
							<td width="15%"><a href="#">等待商家处理</a></td>
							<%
								} else if(orders.getOrdersState() == 1) {
							%>
							<td width="15%"><a href="<%="/FAFCode/SignOrder?order=" + orders.getOrdersNumber() %>" class="oper_btn">签收</a></td>
							<%
								} else if(orders.getOrdersState() == 2) {
							%>
							<td width="15%"><a href="#">商家拒单</a></td>
							<%
								} else if(orders.getOrdersState() == 3){
									System.out.println(orders.getOrdersNumber());
							%>
							<td width="15%"><a href="javascript:void(0)" onclick="comment('<%=orders.getOrdersNumber() %>')" class="oper_btn">我的评价</a></td>
							<%
								}
							%>
						</tr>
					</tbody>
				</table>
				
				<%
					}
				%>

			<%
				int iCountPage = Integer.parseInt(request.getAttribute("page-count").toString());
				int iCurrentPage = Integer.parseInt(request.getAttribute("page").toString());
			%>
			<div class="pagenation">
				<% if(iCurrentPage > 1) {%>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage-1)%>>上一页</a>
				<% } %>
				<!--  -->
				<% if(iCountPage - iCurrentPage <= 0 && iCurrentPage - 4 > 0)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage-4)%>><%=iCurrentPage - 4 %></a>
				<% } %>
				
				<% if(iCountPage - iCurrentPage <= 1 && iCurrentPage - 3 > 0)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage-3)%>><%=iCurrentPage - 3 %></a>
				<% } %>
				<!--  -->		
				<% if(iCurrentPage - 2 > 0)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage-2)%>><%=iCurrentPage - 2 %></a>
				<% } %>
				
				<% if(iCurrentPage - 1 > 0)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage-1)%>><%=iCurrentPage - 1 %></a>
				<% } %>
				
				<a href="#" class="active"><%=iCurrentPage %></a>
				
				<% if(iCountPage - iCurrentPage > 0)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage+1)%>><%=iCurrentPage + 1 %></a>
				<% } %>
				
				<% if(iCountPage - iCurrentPage > 1)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage+2)%>><%=iCurrentPage + 2 %></a>
				<% } %>
				<!--  -->
				<% if(iCurrentPage - 1 <= 0 && iCountPage - iCurrentPage > 2)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage+3)%>><%=iCurrentPage + 3 %></a>
				<% } %>
				
				<% if(iCurrentPage - 2 <= 0 && iCountPage - iCurrentPage > 3)  { %>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage+4)%>><%=iCurrentPage + 4 %></a>
				<% } %>
				<!--  -->
				<% if(iCountPage != iCurrentPage) {%>
				<a href=<%="/FAFCode/MyOrders" + "?" + "page=" + (iCurrentPage+1)%>>下一页</a>
				<% } %>
			</div>
		</div>
	</div>



	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>CopyRight © 2016 北京天天生鲜信息技术有限公司 All Rights Reserved</p>
		<p>电话：010-****888    京ICP备*******8号</p>
	</div>
	<% } %>
</body>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="/FAFCode/UserManagement/js/sweetalert2.bundle.js"></script>
<script>
	function comment(order){
		var res = getState(order);
		if(res == 0)
			doComment(order);
	}

	function getState(order){
		$.ajax({
	   		   method:"post",
	   		   url:"${pageContext.request.contextPath}/TodoComment",
	   		   data: {"action":0, "order":order},
	   		   dataType: "json",
	   		   success: function(data){
	   			   if(data.state == true){
	   				   showComment(data.contect, data.score)
	   				   return;
	   			   }
	   			   else{
						doComment(order)
						return;
	   			   }
	   		   },
	   		   error: function(){
					Swal.showValidationMessage(
						`Request failed: Server NotFound 404`
					);
					return -1;
				}
			})
	}
	
	function showComment(contect, score){
		Swal.fire({
            title: '您对本此交易的评价',
            html:
                '<textarea readonly=true style="padding:10px 10px;min-height:160px;min-width:360px;max-height:160px;max-width:360px;resize: none;" id="swal-input1" class="swal2-input">'+'我的评价：'+contect+'</textarea>'+
                '<input readonly=true type="text" style="min-width:360px;max-width:360px;" id="swal-input2" class="swal2-input" value='+'我的评分：'+score+'>',
            inputAttributes: {
                autocapitalize: 'off'
            },
            showCancelButton: true,
            showConfirmButton: false,
            cancelButtonText: '关闭',
            showLoaderOnConfirm: true
		})
	}
	
	function doComment(order){
		Swal.fire({
            title: '评价此次交易',
            html:
                '<textarea placeholder="您对本次服务的评价" style="padding:10px 10px;min-height:160px;min-width:360px;max-height:160px;max-width:360px;resize: none;" id="swal-input1" class="swal2-input"></textarea>'+
                '<input type="number" placeholder="请为本次服务打分（1-100）" style="min-width:360px;max-width:360px;" id="swal-input2" class="swal2-input" min="1" max="100">',
            inputAttributes: {
                autocapitalize: 'off'
            },
            showCancelButton: true,
            confirmButtonText: '评价',
            cancelButtonText: '取消',
            showLoaderOnConfirm: true,
            preConfirm: (login) => {
            	var contect = document.getElementById("swal-input1").value;
            	if(contect==""){
            		Swal.showValidationMessage(
							`请输入您的评价`
						)
					return;
            	}
            	var score = document.getElementById("swal-input2").value;
            	if(score > 100 || score < 1){
            		Swal.showValidationMessage(
							`请输入有效评分（1-100）`
						)
					return;
            	}
            	$.ajax({
          		   method:"post",
          		   url:"${pageContext.request.contextPath}/TodoComment",
          		   data: {"action":1, "order":order ,"contect":contect, "score":score},
          		   dataType: "json",
          		   success: function(data){
          	            if (data.res) {
          	                Swal.fire({
          	                	type: "success",
          	                	timer: 3000,
          	                    title: `评价成功！`
          	                })
          	            }
          	            else {
          	                Swal.fire({
          	                	type: "error",
          	                	timer: 3000,
          	                    title: `评价失败，请刷新页面后再次尝试`
          	                })
          	            }
          		   },
          		   error: function(){
						Swal.showValidationMessage(
							`Request failed: Server NotFound 404`
						)
          		   }
          		})
            },
            allowOutsideClick: () => !Swal.isLoading()
        })
	}
</script>
</html>